<?php

namespace app\admin\Controller;

use app\admin\model\Article;
use app\admin\model\Category;
use think\App;
use think\Controller;
use think\Db;
use think\facade\View;
use think\Request;

class Articles extends Controller
{
   protected $middleware = ['Auth'];

   public function __construct(App $app = null)
   {
      parent::__construct($app);
      $categories = Category::all_categories();
      View::share(compact('categories'));
   }

   /***
    * 文章列表
    */
   public function index()
   {
      $where = [];
      $paginate = [];
      //按名称
      if ($search = input('get.title')) {
         $where[] = ['title|content', 'like', "%" . $search . "%"];
         $paginate['title'] = $search;
      }

      //按分类
      if ($category_id = input('get.category_id') and $category_id != -1) {
         $where[] = ['category_id', '=', $category_id];
         $paginate['category_id'] = $category_id;
      }

      $articles = Article::with('category', 'photo')
         ->where($where)
         ->order('sort_order', 'desc')
         ->paginate(2, false, ['query' => $paginate]);
      return view('Articles/index', compact('articles'));
   }

   /**
    * 新增文章
    */
   public function create()
   {
      return view('Articles/create');
   }

   /**
    * 保存文章
    */
   public function save(Request $request)
   {
      $data = $request->param();
      $data['create_at'] = date('Y-m-d H:i:s', time());
      $data['update_at'] = date('Y-m-d H:i:s', time());

      Db::table('articles')->strict(false)->insert($data);
      return redirect('Articles/index');
   }

   /**
    * 编辑文章
    */
   public function edit($id)
   {
      $article = Article::with('category', 'photo')->find($id);
      return view('Articles/edit', compact('article'));
   }

   /**
    * 更新文章
    */
   public function update(Request $request, $id)
   {
      $data = $request->param();
      $data['create_at'] = date('Y-m-d H:i:s', time());
      $data['update_at'] = date('Y-m-d H:i:s', time());

      Db::table('articles')->where('id', $id)->strict(false)->update($data);
      return redirect('Articles/index');
   }

   /**
    * 改变状态
    */
   public function change(Request $request)
   {
      $article = Article::find($request->param('id'));
      $attr = $request->param('attr');
      $article->$attr = !$article->$attr;
      $article->save();
   }

   /**
    * 排序
    */
   public function sort(Request $request)
   {
      $sort = Article::find($request->param('id'));
      $sort->sort_order = $request->param('sort');
      $sort->save();
   }

   /**
    * 删除文章
    */
   public function delete($id)
   {
      $article = Db::table('articles')->find($id);
      Db::table('articles')->where('id', $article['id'])->delete();
      //删除对应的图片
      Db::table('photos')->where('id', $article['photo_id'])->delete();
   }

   /**
    * 多选删除
    */
   public function delete_all(Request $request)
   {
      $ids = $request->param('check_id');
      foreach ($ids as $id) {
         $article = Db::table('articles')->find($id);
         Db::table('articles')->where('id', $article['id'])->delete();
         //删除对应的图片
         Db::table('photos')->where('id', $article['photo_id'])->delete();
      }
   }

   /**
    * 导出文章列表
    */
   public function export_article()
   {
      $articles = config('admin.articles');
      $name = '文章列表';
      $data = Article::with('category')
         ->order('create_at', 'desc')
         ->select();
      foreach ($data as &$item) {
         $item['category_id'] = $item['category']['name'];
         $item['is_top'] = 1 ? '是' : '否';
         $item['is_recommend'] = 1 ? '是' : '否';
      }

      export($articles, $data, $name);
   }

}